/*
 * Class: DBConnection
 * 
 * Description: This class contains all the variables and the methods used to initialize
 * 				the database and to make/close the connection.
 * 
 * Version 1.0 Mohamed Ali Ayed - 2011-11-11
 * 		Initial implementation
 * 
 * Version 1.1 Sonny Guenette - 2011-12-08
 * 		Changed variables and parameters names to respect the team naming convention
 * 
 * Copyright GPP - Genuine People Personality inc.
 * 
 */

package dbManager;
import java.sql.*;

public class DBConnection {
	private Connection m_oDBcon;
    private String m_sDBurl;
    private String m_sDBdriver;
    private String m_sDBuser;
    private String m_sDBpassword;
    
    //**************************************************************
	// Method: DBConnection
	// Description: Class constructor
	//**************************************************************
	/**
	 * @param 
	 */
    public DBConnection() {
    	setDBCon(null);
        setDBurl("jdbc:mysql://127.0.0.1:3306/calendar_db");
        setDBDriver("com.mysql.jdbc.Driver");
        setDBUser("root");
        setDBPassword("");
    }
    
    //**************************************************************
	// Method: connectDB
	// Description: Make connection to the database
	//**************************************************************
	/**
	 * @param
	 * @return true if the connection has been successfully established
	 */
	public boolean connectDB (){
		  try {
			  Class.forName(getDBDriver()).newInstance();
			  setDBCon(DriverManager.getConnection(getDBurl(), getDBUser(), getDBPassword()));
			  System.out.println("Connected to the database");
			  } catch (Exception e) {
			  e.printStackTrace();
			  return false;
			  }	
		return true;
	}

	//**************************************************************
	// Method: disconnectDB
	// Description: Close the connection to the database
	//**************************************************************
	/**
	 * @param
	 * @return true if the connection has been successfully closed
	 */
	public boolean disconnectDB(){
		  try {
			  getDBCon().close();
			  System.out.println("Disconnected from database");
			  } catch (Exception e) {
			  e.printStackTrace();
			  return false;
			  }	
			  return true;
	}
	
	/**
	 * @param p_oDBcon the DBcon to set
	 */
	public void setDBCon(Connection p_oDBcon) {
		this.m_oDBcon = p_oDBcon;
	}
	/** 
	 * @return the DB Connection
	 */
	public Connection getDBCon() {
		return m_oDBcon;
	}

	/**
	 * @param p_sDBurl the dbURL to set
	 */
	public void setDBurl(String p_sDBurl) {
		this.m_sDBurl = p_sDBurl;
	}

	/**
	 * @return the DB url
	 */
	public String getDBurl() {
		return m_sDBurl;
	}

	/**
	 * @param p_sDBdriver the dbDriver to set
	 */
	public void setDBDriver(String p_sDBdriver) {
		this.m_sDBdriver = p_sDBdriver;
	}

	/**
	 * @return the DB Driver
	 */
	public String getDBDriver() {
		return m_sDBdriver;
	}

	/**
	 * @param p_sDBuser the dbUser to set
	 */
	public void setDBUser(String p_sDBuser) {
		this.m_sDBuser = p_sDBuser;
	}

	/**
	 * @return the DB User
	 */
	public String getDBUser() {
		return m_sDBuser;
	}

	/**
	 * @param p_sDBpassword the dbPassword to set
	 */
	public void setDBPassword(String p_sDBpassword) {
		this.m_sDBpassword = p_sDBpassword;
	}

	/**
	 * @return the DB Password
	 */
	public String getDBPassword() {
		return m_sDBpassword;
	}
}